﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DotaStrategy.Models;

namespace DotaStrategy.Controllers
{
    
    public class HeroesController : Controller
    {
        MyDotaConnection db = new MyDotaConnection();
   
        public ActionResult Index()
        {
            var heroes = db.Heroes;
            return View(heroes);
        }

        public ActionResult Details(int id)
        {
            Heroe her = db.Heroes.Single(h => h.Id == id);
            return View(her);
        }

        
        public ActionResult Create()
        {
            var heroe = new Heroe();
            return View(heroe);
        } 


        [HttpPost]
        public ActionResult Create(Heroe heroe,FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                db.Heroes.Add(heroe);
                db.SaveChanges();
                return RedirectToAction("Index", "Heroes");
            }

            return View(heroe);
        }
        
        //
        // GET: /Heroes/Edit/5
 
        public ActionResult Edit(int id)
        {
            var heroe = db.Heroes.Single(h => h.Id == id);
            return View(heroe);
        }

        
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var heroe = db.Heroes.Find(id);
            if (TryUpdateModel(heroe)) {
                db.SaveChanges();
                return RedirectToAction("Index", "Heroes");
            }
            return View(heroe);
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            var heroe = db.Heroes.Find(id);
            var name = heroe.Name;
            db.Heroes.Remove(heroe);
            db.SaveChanges();
            return JavaScript("alert('Heroe \""+ name+ "\" has been killed')");

        }
        
    }
}
